#/usr/bin/env python
"""
The game of Gobhopper
"""


#Import Modules
import os, pygame
import actors
import setup
import input
from lib import keyboard
from pygame.locals import *

def load_level(maxWidth):
    #HACK: Make hardcoded level (Sun Dec  2 19:09:54 EST 2007)
    gobs = []
    for a in range(15):
        gobs.append( actors.Gob() )
    x = 100
    y = 100
    r = -20
    increment = 150
    inc_change = 50
    for g in gobs:
        print "gob at", x,(y-r)
        g.move_abs((x,y-r))
        x += increment
        r *= -1

        increment += inc_change
        if increment > 200:
            inc_change *= -1
        elif increment <= 150:
            inc_change *= -1

        if (x + g.rect.width) > maxWidth:
            y += 200    # goto next line
            x = 100     # goto beginning of line

    return gobs


def main():
    """this function is called when the program starts.
       it initializes everything it needs, then runs in
       a loop until the function returns."""
#Initialize Everything
    pygame.init()
    screen = pygame.display.set_mode((1024, 668))
    pygame.display.set_caption('Gobhopper')
    pygame.mouse.set_visible(1)

#Create The Backgound
    background = pygame.Surface(screen.get_size())
    background = background.convert()
    background.fill((250, 250, 250))

#Put Text On The Background, Centered
    if pygame.font:
        font = pygame.font.Font(None, 36)
        text = font.render("Gobs!!!", 1, (10, 10, 10))
        textpos = text.get_rect(centerx=background.get_width()/2)
        background.blit(text, textpos)
    else:
        print "NO FONTS!"

#Display The Background
    screen.blit(background, (0, 0))
    pygame.display.flip()

#Prepare Game Objects
    clock = pygame.time.Clock()

    gobs = load_level(background.get_width())

    player = actors.Player(gobs[0])

    gobGroup = pygame.sprite.Group( gobs )
    playerGroup = pygame.sprite.Group( player )


    keyRemember = keyboard.KeyboardRemember((K_SPACE,))
#Main Loop
    while 1:
        clock.tick(60)
        player.tick()

    #Handle Input Events
        lastDirKey = -1     # something invalid
        for event in pygame.event.get():
            if event.type == QUIT:
                return
            elif event.type is KEYDOWN and (event.key == K_ESCAPE or event.key == K_q):
                return
            elif event.type is KEYDOWN and event.key == K_d:
                gob.print_debug()

            keyRemember.update(event)
            if not input.handleInputForPlayer( keyRemember.getCurrent(), player ):
                print "didn't handle it"

        for p in playerGroup:
            sp_list = pygame.sprite.spritecollide( p, gobGroup, False )
            if len(sp_list) > 0:
                p.collide( sp_list[0] )

        gobGroup.update()
        playerGroup.update()

    #Draw Everything
        screen.blit(background, (0, 0)) # blank out the whole screen (for now)
        gobGroup.draw(screen)
        playerGroup.draw(screen)
        pygame.display.flip()

#Game Over


#this calls the 'main' function when this script is executed
if __name__ == '__main__': main()

